﻿using System.Collections.Generic;
using System.Xml.Serialization;

namespace CommonLibrary.Models.Profiles
{
    public class ApplicationProfileConfig : XmlConfigFile
    {
        [XmlElement]
        public string Description;

        [XmlArray("Profiles")]
        [XmlArrayItem("Profile", typeof(Profile))]
        public List<Profile> Profiles { get; set; }

        public ApplicationProfileConfig()
        {
            if (Profiles == null) Profiles = new List<Profile>();
        }

        [XmlArray("ConfigActionsMap")]
        [XmlArrayItem("ConfigAction", typeof(CustomAction))]
        public List<CustomAction> ConfigActionsMap { get; set; }
    }

    public class Profile
    {
        [XmlAttribute]
        public int Id;

        [XmlArrayItem("Button", typeof(CustomAction))]
        public List<CustomAction> ButtonsMap { get; set; }

        [XmlArrayItem("Axis", typeof(CustomAction))]
        public List<CustomAction> AxisMap { get; set; }

        [XmlArrayItem("SpecialAction", typeof(CustomAction))]
        public List<CustomAction> SpecialActionsMap { get; set; }

        public Profile()
        {
            if (ButtonsMap == null) ButtonsMap = new List<CustomAction>();
            if (AxisMap == null) AxisMap = new List<CustomAction>();
            if (SpecialActionsMap == null) SpecialActionsMap = new List<CustomAction>();
        }
    }

    // ---

    public class CustomAction
    {
        [XmlAttribute]
        public string Name;

        [XmlArray("Triggers")]
        [XmlArrayItem("Trigger", typeof(ActionEvent))]
        public List<ActionEvent> Trigger { get; set; }

        public CustomAction()
        {
            if (Trigger == null) Trigger = new List<ActionEvent>();
        }
    }

    // ---

    public class ActionEvent
    {
        [XmlAttribute]
        public TypeOfAction Type;

        [XmlAttribute]
        public string Id;

        [XmlAttribute]
        public double Parameter;
        [XmlIgnore]
        public bool ParameterSpecified { get { return Type == TypeOfAction.SpecialButton; } }

        [XmlAttribute]
        public string Target;
        [XmlIgnore]
        public bool TargetSpecified { get { return Type == TypeOfAction.SpecialButton; } }
        
        [XmlAttribute]
        public string OpositeId;
        [XmlIgnore]
        public bool OpositeSpecified { get { return Type == TypeOfAction.ButtonAxis; } }

        [XmlAttribute]
        public double DeadZone;
        [XmlIgnore]
        public bool DeadZoneSpecified { get { return Type == TypeOfAction.Axis; } }

        [XmlAttribute]
        public double Multiplier;
        [XmlIgnore]
        public bool MultiplierSpecified { get { return Type == TypeOfAction.Axis; } }

        [XmlAttribute]
        public double Exponenet;
        [XmlIgnore]
        public bool ExponenetSpecified { get { return Type == TypeOfAction.Axis; } }

        [XmlAttribute]
        public ShapeOfDeadZone Shape;
        [XmlIgnore]
        public bool ShapeSpecified { get { return Type == TypeOfAction.Axis; } }
    }


    public enum TypeOfAction
    {
        Button,
        ButtonAxis,
        SpecialButton,
        Axis
    }

    public enum ShapeOfDeadZone
    {
        Circle,
        Ellipse,
        Square
    }
}
